// let jsApiList=[];
let voicelist = [];
let uploadlist = [];
$(function () {
	// let vConsole = new VConsole();
	$.getScript("../js/jweixin-sgccigw-1.0.0.js" /*"js/jweixin-1.0.0.js"*/, function () {
		'use strict';
		wx.ready(function () {
		});
		jsApiList = [
			'getZipAppDirectory',

			'ext_FileManager_Write',
			'ext_FileManager_Read',
			'ext_FileManager_Remove',
			'ext_FileManager_Create',
			'ext_FileManager_RemoveDirectory',
			'ext_FileManager_Exist',
			'ext_FileManager_IsDirectory',
			'ext_FileManager_Information',

			'ext_Socket_Init',
			'ext_Socket_UnInit',
			'ext_Socket_CreateChannel',
			'ext_Socket_CloseChannel',
			'ext_Socket_SendData',

			'multiWindows_subscribe',

			'ext_SGMap_init',
			'ext_SGMap_Operation',
			'ext_SGMap_Search',
			'ext_SGMap_Location',

			'ext_Etas_List',
			'ext_Etas_Init',
			'ext_Etas_Reg',
			'ext_Etas_UnReg',
			'ext_Etas_checkStatus',
			'ext_Etas_Verify',
			'ext_Etas_SaveData',
			'ext_Etas_GetData',
			'ext_Etas_RemoveData',
			'ext_Etas_Clear',
			'ext_Etas_OffLineVerify',

			'ext_ISCP_Init',
			'ext_ISCP_ConnectService',
			'ext_ISCP_GetLocalPort',
			'ext_ISCP_Close',
			'ext_ISCP_Status',

			'ext_Database_Open',
			'ext_Database_Exec',
			'ext_Database_Query',
			'ext_Database_Close',

			'ext_rlble_initBluetoothAdapter',
			'ext_rlble_scanBluetoothDev',
			'ext_rlble_startConnectBluetoothDev',
			'ext_rlble_onBluetoothDevConnDataRecv',
			'ext_rlble_BluetoothDevSendData',
			'ext_rlble_closeBluetoothDevConnection',
			'ext_rlble_stopBluetoothDevScan',

			'ext_Popover_Open',
			'ext_Popover_Close',

			"multiWindows_open",
			"multiWindows_close",
			"multiWindows_startWidget",
			"multiWindows_finishWidget",
			"multiWindows_subscribe",
			"multiWindows_publish",

			'selectEnterpriseContact',
			'openChatWithMsg',
			'openUserProfile',

			'onMenuShareAppMessage',
			'onMenuShareWechat',
			'shareAppMessage',
			'shareWechatMessage',
			'onMenuShareTimeline',

			'chooseImage',
			'getLocalImgData',
			'previewImage',
			'uploadImage',
			'downloadImage',
			'previewFile',

			'chooseVideo',
			'uploadVideo',
			'downloadVideo',

			'startRecord',
			'stopRecord',
			'onVoiceRecordEnd',
			'playVoice',
			'playVoice',
			'stopVoice',
			'onVoicePlayEnd',
			'startRecordVoiceBuffer',
			'onRecordBufferReceived',
			'stopRecordVoiceBuffer',
			'uploadVoice',
			'downloadVoice',
			'translateVoice',

			'startWifi',
			'stopWifi',
			'connectWifi',
			'getWifiList',
			'onGetWifiList',
			'onWifiConnected',
			'getConnectedWifi',

			'openBluetoothAdapter',
			'closeBluetoothAdapter',
			'getBluetoothAdapterState',
			'onBluetoothAdapterStateChange',
			'startBluetoothDevicesDiscovery',
			'stopBluetoothDevicesDiscovery',
			'getBluetoothDevices',
			'onBluetoothDeviceFound',
			'getConnectedBluetoothDevices',
			'createBLEConnection',
			'closeBLEConnection',
			'onBLEConnectionStateChange',
			'getBLEDeviceServices',
			'getBLEDeviceCharacteristics',
			'readBLECharacteristicValue',
			'writeBLECharacteristicValue',
			'notifyBLECharacteristicValueChange',
			'onBLECharacteristicValueChange',

			'setClipboardData',
			'getClipboardData',

			'getNetworkType',
			'onNetworkStatusChange',

			'openLocation',
			'getLocation',
			'startAutoLBS',
			'stopAutoLBS',
			'onLocationChange',

			'onHistoryBack',
			'hideOptionMenu',
			'showOptionMenu',
			'closeWindow',
			'hideMenuItems',
			'showMenuItems',
			'hideAllNonBaseMenuItem',
			'showAllNonBaseMenuItem',
			'onUserCaptureScreen',
			'openUrl',

			'scanQRCode',

			'getStepCount',
			'getAllPhoneContacts',
			'addCalendarEvent',

			'showWatermark',
			'hideWatermark',

			'checkIsSupportSoterAuthentication',
			'checkIsSoterEnrolledInDevice',
			'startSoterAuthentication',
			'bioassayAuthentication',

			'launch3rdApp',
			'request3rdApp',
			'getInstallState',

			'ocr',

			'checkJsApi',

			'ext_gislocation_init',
			'ext_gislocation_start',
			'ext_gislocation_stop',

			'ext_SandBox_Reg',
			'ext_SandBox_StartSubApp',
			'ext_SandBox_CloseSubApp',
			'ext_SandBox_ScreenPolicy',
			'ext_SandBox_isRoot',
			'ext_SandBox_isSimulator',
			'ext_SandBox_WaterMark',
			'ext_SandBox_ScreenShot',
			'ext_SandBox_DelePhoto',
			'ext_SandBox_UploadShot',

			'ext_wxlog_setLogOpen',
			'ext_wxlog_getLogFile',
			'ext_SGMap_init',
			'ext_SGMap_Location',

			'ext_DataCache_Get',
			'ext_DataCache_Save',
			'ext_DataCache_SaveInfo',
			'ext_DataCache_GetInfo',
			'ext_DataCache_RemoveInfo',

			'ext_Net_Upload',
			'ext_Net_CancelUpload',
			'ext_Net_Download',
			'ext_Net_SimpleDownload',
			'ext_Net_MultiDownload',

			'ext_screenControl_direction',

			'ext_WeMeet_Init',
			'ext_WeMeet_Login',
			'ext_WeMeet_Logout',
			'ext_WeMeet_Join',

			'ext_DeviceInfo_GetInfo',

			'ext_Compressor_Size',
			'ext_Compressor_Quality',

			'ext_OfflineFaceRec_add',
			'ext_OfflineFaceRec_delete',
			'ext_OfflineFaceRec_recognition',
			'ext_OfflineFaceRec_capture',

			'ext_unzip_untie',
			'ext_unzip_compression',
			'ext_Intent_Dial'
		];
		wx.config({
			beta: true, // 调用wx.invoke形式的接口值时，该值必须为true。
			debug: false, // 开启调试模式,调用的所有api的返回值会在客户端alert出来，若要查看传入的参数，可以在pc端打开，参数信息会通过log打出，仅在pc端时才会打印。
			appId: corpid, // 必填，政务微信的cropID
			timestamp: '1626832073', // 必填，生成签名的时间戳
			nonceStr: nonceStr, // 必填，生成签名的随机串
			signature: signature, // 必填，签名，见附录1
			jsApiList: jsApiList // 必填，需要使用的JS接口列表，所有JS接口列表见附录2
		});
	});

	$(".record").click(function () {
		if (!$(this).hasClass("stop")) {
			$(this).addClass("stop");
			let i = 0;
			$(this).data("interval", window.setInterval(function () {
				$(".record-time").html((++i) + "s");
			}, 1000, i));
			wx.startRecord();
			let me = this;
			wx.onVoiceRecordEnd({
				complete: function (res) {
					let localId = res.localId;
					if (localId) {
						if (voicelist.findIndex((item) => item == localId) == -1) {
							voicelist.push(localId);
							listVoiceFile(voicelist);
						}
					}
					$(me).click();
				}
			});
		} else {
			$(this).removeClass("stop");
			window.clearInterval($(this).data("interval"));
			$(".record-time").html("");
			wx.stopRecord({
				success: function (res) {
					let localId = res.localId;
					if (localId) {
						if (voicelist.findIndex((item) => item == localId) == -1) {
							voicelist.push(localId);
							listVoiceFile(voicelist);
						}
					}
				}
			});
		}
	});

	$(".play").click(function () {
		if (!$(this).hasClass("play-disabled") && voicelist.currentIndex) {
			$(this).addClass("play-disabled");
			$(this).removeClass("play");
			$(this).next().removeClass("suspend-disabled").addClass("suspend");
			$(this).next().next().removeClass("stop-disabled").addClass("stop");
			let me = this;
			me.i = me.i || 0;
			if (!$(this).data("interval")) {
				$(this).data("interval", window.setInterval(function () {
					$(".play-time").html((++me.i) + "s");
				}, 1000, me.i));
			};
			wx.playVoice({
				localId: (voicelist.currentIndex ? voicelist.currentIndex : "")
			});
			wx.onVoicePlayEnd({
				success: function (res) {
					let localId = res.localId;
					$(me).next().next().click();
				}
			});
		} else {
			alert("请点击音频文件列表中要播放的文件");
		}
	});

	$(".stop-disabled").click(function () {
		if ($(this).hasClass("stop")) {
			$(this).removeClass("stop").addClass("stop-disabled");
			$(this).prev().removeClass("suspend").addClass("suspend-disabled");
			$(this).prev().prev().removeClass("play-disabled").addClass("play");
			if ($(this).prev().prev().data("interval")) {
				window.clearInterval($(this).prev().prev().data("interval"));
				$(this).prev().prev().removeData("interval");
				$(this).prev().prev().get(0).i = 0;
				$(".play-time").html("");
			}
			wx.stopVoice({
				localId: (voicelist.currentIndex ? voicelist.currentIndex : "")
			});
		}
	});

	$(".suspend-disabled").click(function () {
		if ($(this).hasClass("suspend")) {
			$(this).prev().removeClass("play-disabled").addClass("play");
			$(this).removeClass("suspend").addClass("suspend-disabled");
			if ($(this).prev().data("interval")) {
				window.clearInterval($(this).prev().data("interval"));
				$(this).prev().removeData("interval");
			}
			wx.pauseVoice({
				localId: (voicelist.currentIndex ? voicelist.currentIndex : "")
			});
		};
	});

	$(".voice-list").on("click", "li", function () {
		if ($(this).data("localId")) {
			voicelist.currentIndex = $(this).data("localId");
		}
	});

	$(".upload").click(function () {
		if (voicelist.currentIndex) {
			let index = voicelist.findIndex((item) => item == voicelist.currentIndex);
			wx.uploadVoice({
				localId: voicelist.currentIndex,
				isShowProgressTips: 1,
				success: function (res) {
					uploadlist[index] = res.serverId;
					alert("上传音频文件成功");
				}
			});
		} else {
			alert("请点击音频文件列表中要上传的文件");
		}
	});

	$(".download").click(function () {
		if (voicelist.currentIndex) {
			let index = voicelist.findIndex((item) => item == voicelist.currentIndex);
			if (uploadlist[index]) {
				wx.downloadVoice({
					serverId: uploadlist[index],
					isShowProgressTips: 1,
					success: function (res) {
						alert("下载音频文件成功");
					}
				});
			} else {
				alert("此音频文件还没有上传或者正在上传当中，请稍候重试");
			}
		} else {
			alert("请上传音频文件列表中的文件，再进行下载操作。");
		}
	});

	$(".text").click(function () {
		if (voicelist.currentIndex) {
			wx.translateVoice({
				localId: voicelist.currentIndex,
				isShowProgressTips: 1,
				success: function (res) {
					$(".text-content").html(res.translateResult);
					$(".text-wrapper").show();
				}
			});
		} else {
			alert("请点击音频文件列表中转换的音频文件");
		}
	});
});

function listVoiceFile(voices) {
	for (let voice of voices) {
		let isExist = false;
		$(".voice-list").children().each(function () {
			if ($(this).data("localId") == voice) {
				isExist = true;
			}
		});
		if (!isExist) {
			let li = $("<li>音频-" + voice + "</li>");
			li.data("localId", voice);
			$(".voice-list").append(li);
		}
	}
}